package com.zxy.paycenter.modules.alipay.controller;

import com.zxy.paycenter.common.aop.RequireSignature;
import com.zxy.paycenter.common.constants.Constant;
import com.zxy.paycenter.common.constants.ResponseCodeConstant;
import com.zxy.paycenter.common.message.request.*;
import com.zxy.paycenter.modules.BaseController;
import com.zxy.paycenter.modules.alipay.service.AlipayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Properties;

/**
 * @author zxy
 */
@Slf4j
@RestController
@Api(tags = "支付宝支付Rest")
@RequestMapping(value = "/alipay")
public class AlipayRestController extends BaseController {
    @Resource
    private AlipayService aliPayService;

    @RequireSignature
    @ApiOperation(value = "商户代扣")
    @PostMapping(value = "/merchantWithhold")
    public Object merchantWithhold(HttpServletRequest request, ModelMap modelMap, CommodityReq commodityReq) {
        Properties properties = (Properties) request.getAttribute("aliConfig");
        String merchantName = properties.getProperty("merchantName");
        log.info("{}:商户代扣", merchantName);
        String orderString = aliPayService.withholdPay(commodityReq, properties);
        modelMap.addAttribute("orderString", orderString);
        log.info("响应信息:{}", orderString);
        return orderString;
    }

    @RequireSignature
    @ApiOperation(value = "支付宝订单查询")
    @PostMapping(value = "/alipayTradeQuery")
    public Object alipayTradeQuery(HttpServletRequest request, TradeReq tradeReq) {
        Properties properties = (Properties) request.getAttribute("aliConfig");
        String merchantName = properties.getProperty("merchantName");
        log.info("{}:支付宝订单查询", merchantName);
        String orderString = aliPayService.alipayTradeQuery(tradeReq, properties);
        log.info("响应信息:{}", orderString);
        if (Constant.FAIL.equals(orderString)) {
            log.info("支付宝订单查询失败");
            return packProducterSuccessResponse(ResponseCodeConstant.FAIL, "支付宝订单查询失败", new String[]{}, new Object[]{});
        }
        return packProducterSuccessResponse(new String[]{"orderString"}, new Object[]{orderString});
    }

    @RequireSignature
    @ApiOperation(value = "支付宝订单退款")
    @PostMapping(value = "/alipayTradeRefund")
    public Object alipayTradeRefund(HttpServletRequest request, TradeRefundReq tradeRefundReq) {
        Properties properties = (Properties) request.getAttribute("aliConfig");
        String merchantName = properties.getProperty("merchantName");
        log.info("{}:支付宝订单退款", merchantName);
        String orderString = aliPayService.alipayTradeRefund(tradeRefundReq, properties);
        log.info("响应信息:{}", orderString);
        if (Constant.FAIL.equals(orderString)) {
            log.info("支付宝订单退款失败");
            return packProducterSuccessResponse(ResponseCodeConstant.FAIL, "支付宝订单退款失败", new String[]{}, new Object[]{});
        }
        return packProducterSuccessResponse(new String[]{"orderString"}, new Object[]{orderString});
    }

    @RequireSignature
    @ApiOperation(value = "支付宝订单退款信息查询")
    @PostMapping(value = "/alipayTradeRefundInfo")
    public Object alipayTradeRefundInfo(HttpServletRequest request, TradeRefundReq tradeRefundReq) {
        Properties properties = (Properties) request.getAttribute("aliConfig");
        String merchantName = properties.getProperty("merchantName");
        log.info("{}:支付宝订单退款信息查询", merchantName);
        String orderString = aliPayService.alipayTradeRefundInfo(tradeRefundReq, properties);
        log.info("响应信息:{}", orderString);
        if (Constant.FAIL.equals(orderString)) {
            log.info("支付宝订单退款信息查询失败");
            return packProducterSuccessResponse(ResponseCodeConstant.FAIL, "支付宝订单退款信息查询失败", new String[]{}, new Object[]{});
        }
        return packProducterSuccessResponse(new String[]{"orderString"}, new Object[]{orderString});
    }

    @RequireSignature
    @ApiOperation(value = "支付宝订单关闭")
    @PostMapping(value = "/alipayTradeClose")
    public Object alipayTradeClose(HttpServletRequest request, TradeCloseReq tradeCloseReq) {
        Properties properties = (Properties) request.getAttribute("aliConfig");
        String merchantName = properties.getProperty("merchantName");
        log.info("{}:支付宝订单关闭", merchantName);
        String orderString = aliPayService.alipayTradeClose(tradeCloseReq, properties);
        log.info("响应信息:{}", orderString);
        if (Constant.FAIL.equals(orderString)) {
            log.info("支付宝订单关闭失败");
            return packProducterSuccessResponse(ResponseCodeConstant.FAIL, "支付宝订单关闭失败", new String[]{}, new Object[]{});
        }
        return packProducterSuccessResponse(new String[]{"orderString"}, new Object[]{orderString});
    }

    @RequireSignature
    @ApiOperation(value = "用户支付宝签约")
    @PostMapping(value = "/userAgreementSign")
    public Object userAgreementSign(HttpServletRequest request, MemberReq memberReq) {
        Properties properties = (Properties) request.getAttribute("aliConfig");
        String merchantName = properties.getProperty("merchantName");
        log.info("{}:用户支付宝签约，memberId:{}", merchantName, memberReq.getMemberId());
        String memberString = aliPayService.userAgreementSign(memberReq, properties);
        log.info("响应信息:{}", memberString);
        if (Constant.FAIL.equals(memberString)) {
            log.info("用户支付宝签约失败");
            return packProducterSuccessResponse(ResponseCodeConstant.FAIL, "用户支付宝签约失败", new String[]{}, new Object[]{});
        }
        return packProducterSuccessResponse(new String[]{"agreementUrl"}, new Object[]{memberString});
    }

    @RequireSignature
    @ApiOperation(value = "用户查询支付宝签约信息")
    @PostMapping(value = "/userQueryAgreementSign")
    public Object userQueryAgreementSign(HttpServletRequest request, MemberQueryReq memberQueryReq) {
        Properties properties = (Properties) request.getAttribute("aliConfig");
        String merchantName = properties.getProperty("merchantName");
        log.info("{}:用户查询支付宝签约信息，agreementNo:{}", merchantName, memberQueryReq.getAgreementNo());
        String memberString = aliPayService.userQueryAgreementSign(memberQueryReq, properties);
        log.info("响应信息:{}", memberString);
        if (Constant.FAIL.equals(memberString)) {
            log.info("用户支付宝查询签约信息失败");
            return packProducterSuccessResponse(ResponseCodeConstant.FAIL, "用户支付宝查询签约信息失败", new String[]{}, new Object[]{});
        }
        return packProducterSuccessResponse(new String[]{"agreementUrl"}, new Object[]{memberString});
    }

    @RequireSignature
    @ApiOperation(value = "用户解绑支付宝签约")
    @PostMapping(value = "/userUnAgreementSign")
    public Object userUnAgreementSign(HttpServletRequest request, MemberQueryReq memberQueryReq) {
        Properties properties = (Properties) request.getAttribute("aliConfig");
        String merchantName = properties.getProperty("merchantName");
        log.info("{}:用户解绑支付宝签约，agreementNo:{}", merchantName, memberQueryReq.getAgreementNo());
        String memberString = aliPayService.userUnAgreementSign(memberQueryReq, properties);
        log.info("响应信息:{}", memberString);
        if (Constant.FAIL.equals(memberString)) {
            log.info("用户解绑支付宝签约失败");
            return packProducterSuccessResponse(ResponseCodeConstant.FAIL, "用户解绑支付宝签约失败", new String[]{}, new Object[]{});
        }
        return packProducterSuccessResponse(new String[]{}, new Object[]{});
    }
}
